package com.hex.liyue.giftmodule.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hex.liyue.R;
import com.hex.liyue.commonmodule.activity.BaseActivity;
import com.hex.liyue.commonmodule.utils.L;
import com.hex.liyue.commonmodule.utils.StatusBarCompatUtils;
import com.hex.liyue.commonmodule.view.recycleview.YRecycleview;
import com.hex.liyue.giftmodule.adapter.AdapterGiveGifts;
import com.hex.liyue.giftmodule.bean.GiveGiftsBean;
import com.hex.liyue.giftmodule.presenter.GiveGiftsPresenter;
import com.hex.liyue.giftmodule.presenter.IGiveGiftsView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 赠送礼物的界面
 */
public class GiveGiftsActivity extends BaseActivity implements IGiveGiftsView, YRecycleview.RefreshAndLoadMoreListener {
    @Bind(R.id.iv_toobar_back)
    ImageView ivToobarBack;
    @Bind(R.id.tv_toobar_title)
    TextView tvToobarTitle;
    @Bind(R.id.rv_harvest_gifts)
    YRecycleview rvHarvestGifts;
    @Bind(R.id.empty_gift)
    LinearLayout emptyGift;

    private GiveGiftsPresenter giveGiftsPresenter;
    private int page = 1, page_num = 10;
    private List<GiveGiftsBean.DataBean> list = new ArrayList<>();
    private AdapterGiveGifts adapterGiveGifts;

    @Override
    protected void bindPresenter() {
        giveGiftsPresenter = new GiveGiftsPresenter(this);
    }

    @Override
    protected void initData(Bundle extras) {
        giveGiftsPresenter.getGiveGifts(page, page_num);
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_give_gifts;
    }

    @Override
    protected void initToolbar() {
        StatusBarCompatUtils.compat(this);
        tvToobarTitle.setText("赠送的礼物");
        ivToobarBack.setVisibility(View.VISIBLE);
    }


    @OnClick({R.id.iv_toobar_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_toobar_back:
                onBackPressed();
                break;
        }
    }


    @Override
    public void setAdapter(final GiveGiftsBean giveGiftsBean) {
        if (page == 1) {
            list.clear();
        }
        if (giveGiftsBean != null) {
            if (giveGiftsBean.data.size() == 0 && page > 1) {
                page--;
            }
            for (GiveGiftsBean.DataBean dataBean : giveGiftsBean.data) {
                list.add(dataBean);
            }
        }
        if (adapterGiveGifts == null) {
            adapterGiveGifts = new AdapterGiveGifts();
            adapterGiveGifts.setOnItemClickListener(new AdapterGiveGifts.OnItemClickListener() {
                @Override
                public void onItemClickListener(int position) {
                    GiveGiftsBean.DataBean dataBean = list.get(position);
                    Intent intent = new Intent(GiveGiftsActivity.this, GiveGiftDetailsActivity.class);
                    intent.putExtra("dataBean", dataBean);
                    startActivity(intent);
                }
            });
            rvHarvestGifts.setLayoutManager(new LinearLayoutManager(this));
            rvHarvestGifts.setRefreshAndLoadMoreListener(this);
            rvHarvestGifts.setEmptyView(emptyGift);
            rvHarvestGifts.setAdapter(adapterGiveGifts);
        }
        adapterGiveGifts.setList(list);
        adapterGiveGifts.notifyDataSetChanged();
        rvHarvestGifts.reSetStatus();
        L.d("WJ", page + "=size=" + giveGiftsBean.data.size());
    }

    @Override
    public void showLod(String msg) {
        showLoad(GiveGiftsActivity.this, msg);
    }

    @Override
    public void closeLod() {
        closeLoad();
    }

    @Override
    public void showT(String msg) {
        showToast(GiveGiftsActivity.this, msg);
    }

    @Override
    public void onRefresh() {
        page = 1;
        giveGiftsPresenter.getGiveGifts(page, page_num);
    }

    @Override
    public void onLoadMore() {
        page++;
        giveGiftsPresenter.getGiveGifts(page, page_num);
    }

}
